/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-27     balanceTWK   first version
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include "sht20.h"
/* defined the LED0 pin: PC0 */
#define LED_BLUE_PIN    	GET_PIN(C, 10)
#define LED_YELLOW_PIN    	GET_PIN(A, 12)
#define LED_GREEN_PIN    	GET_PIN(C, 8)
#define LED_RED_PIN    		GET_PIN(C, 7)

int main(void)
{
    int count = 1;
    float humidity, temperature;
    sht20_device_t dev;                /* device object */
    const char *i2c_bus_name = "i2c1"; /* i2c bus station */
    /* set LED0 pin mode to output */
    rt_pin_mode(LED_RED_PIN, PIN_MODE_OUTPUT);

    dev = sht20_init(i2c_bus_name);
    if(dev == RT_NULL)
    {
        rt_kprintf(" The sensor initializes failure");
        return 0;
    }
    while (count++)
    {
        rt_pin_write(LED_RED_PIN, PIN_HIGH);
        rt_thread_mdelay(500);
        rt_pin_write(LED_RED_PIN, PIN_LOW);
        rt_thread_mdelay(500);
    
            /* read humidity */
        humidity = sht20_read_humidity(dev);
        rt_kprintf("humidity   : %d.%d %%\n", (int)humidity, (int)(humidity * 10) % 10); /* former is integer and behind is decimal */

        /* read temperature */
        temperature = sht20_read_temperature(dev);
        rt_kprintf("temperature: %d.%d \n", (int)temperature, (int)(temperature * 10) % 10); /* former is integer and behind is decimal */
    }

    return RT_EOK;
}
